﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.ComponentModel;

namespace ivvorigemRecursos
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {              
                bindGridView();
            }
            TxtNome.Focus();            
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    object objTemp = GridView1.DataKeys[e.Row.RowIndex].Value as object;
                    if (objTemp != null)
                    {
                        string id = objTemp.ToString();

                        string sql1 = "";
                        sql1 = sql1 + "SELECT * FROM OrigemRecurso WHERE (codigoOrigemRecurso = " + id + ")";

                        System.Data.SqlClient.SqlConnection conexao = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                        conexao.Open();

                        System.Data.SqlClient.SqlCommand cmdT = new System.Data.SqlClient.SqlCommand(sql1, conexao);
                        System.Data.SqlClient.SqlDataReader drT = cmdT.ExecuteReader();

                        string codigoOrigemRecurso = "";
                        string descricaoOrigemRecurso = "";
                        int count = 0;

                        if (drT.HasRows)
                        {
                            while (drT.Read())
                            {
                                codigoOrigemRecurso = drT["codigoOrigemRecurso"].ToString();
                                descricaoOrigemRecurso = drT["descricaoOrigemRecurso"].ToString();

                                if (count == 0)
                                {
                                    string editar = "<a href=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/origemRecursos/Edit.aspx?codigoOrigemRecurso=" + codigoOrigemRecurso + "><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btEditar(2).gif alt=Editar title=Editar /></a>";
                                    e.Row.Cells[2].Controls.Add(new LiteralControl(editar));

                                    string remover = "<a href=javascript:window.open('" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/deletarOrigemRecurso.aspx?codigoOrigemRecurso=" + codigoOrigemRecurso + "','usuario','location=0,resizable=0,status=0,scrollbars=0,width=350,height=150');><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btExcluirMacroProcesso.gif alt=Excluir title=Excluir /></a>";
                                    e.Row.Cells[2].Controls.Add(new LiteralControl(remover));
                                }
                                count++;
                            }
                        }
                        drT.Close();
                        cmdT.Dispose();
                        conexao.Close();
                    }
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bindGridView();
        }

        public void bindGridView()
        {
            string sql1 = "";
            sql1 = sql1 + "SELECT * FROM OrigemRecurso WHERE UPPER (descricaoOrigemRecurso) LIKE '%" + TxtNome.Text.ToUpper() + "%'";

            string connString = ConfigurationManager.AppSettings["baseDados"];

            SqlDataAdapter dAdapter = new SqlDataAdapter(sql1, connString);

            SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

            DataTable dTable = new DataTable();

            dAdapter.Fill(dTable);

            GridView1.DataSource = dTable;

            dTable.Columns.Add("Açoes");

            GridView1.DataBind();

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            bindGridView();
        }
    }
}
